import app from "../../app";
import { Tpl_propDefault_look, template } from "../../shared/master/MsgAction";
import { player } from "../fight/body/player";
import { face_prop_item, prop_min_type } from "../../shared/interface";
import { ServiceType } from "../../shared/master/serviceProto";
import { logger } from "../server/logger";

/**
 * 需要实现
 * 使用 - 执行不同逻辑  可叠加  使用 + 数量
 * 查看 - 执行不同UI逻辑
 * 出售 - 计算出对应价值 增加到用户钱包
 * 丢弃 - 丢弃到当前位置
 * 拍卖 - 拍卖 + 道具IDs + 数量s + 出售金币
 */

export enum prop_type {
    prop,
    equip
}


export class prop_base implements face_prop_item {
    cfg_id: string = app.v4();
    uuid: string = app.v4();
    from: string = '未知'
    /**
     * 道具类型
     */
    min_type: prop_min_type;
    /**
     * 是否可叠加
     */
    haveSup: boolean;
    score: number;
    num: number;
    data: any;
    value: number = 0;
    icon:string = '✨';
    constructor(data: face_prop_item) {
        this.uuid = data.uuid || this.uuid;
        this.min_type = data.min_type;
        this.num = data.num;
        this.score = data.score;
        this.from = data.from;
        this.haveSup = true;
        this.data = data.data;
        if (this.data) {
            this.icon = this.data.icon;
            this.cfg_id = this.data.id;
        } else {
            debugger;
        }
        if (typeof (this.data.value) == 'number') {
            this.value = this.data.value
        } else {
            this.value = this.score;
        }

    }
    getStyleName(){
        return `${this.icon || '❔'}${this.data.name}`;
    }
    getStrName() {
        return this.data.name;
    }
    /**
     * 使用
     */
    use(tag: player, num: number) {

        // let key = this.data.instruct as <T extends string & keyof ServiceType['api']>
        const instructKey = this.data.instruct_sys as string;
        if (instructKey) {
            tag._callApi(`bag/function/${instructKey}`, {
                num: num,
                id: this.cfg_id
            })
        } else {
            tag.sendMsg(template.default, '此物品无法直接使用,请查看物品描述以获得详情')
        }

        return false;
    }
    /**
     * 查看道具
     * 返回 消息模板data
     * 
     */
    look(tag: player): { template: template, data: any } {
        let data = this.data;
        let tpl: Tpl_propDefault_look = {
            name: data.name,
            from: this.from,
            score: this.value,
            cont: this.num,
            type: this.min_type,
            decs: data.desc,
            icon: "",
            instruct_tips: data.instruct_tips,
            instruct: data.instruct
        }
        return { template: template.prop_info, data: tpl }
    }
    set_cont(num: number) {
        this.num = num;
    }
    cont_change(change: number) {
        if (change == 0) {
            return false;
        }
        if (change > 0) {
            this.num += change;
        } else {
            if (this.num + change < 0) {
                logger.error('cont_change 异常')
                this.num = 0;
                return false;
            }
            this.num += change;
        }
        return true;
    }
    /**
     * 出售
     */
    destroy() {
        this.num = 0;
    }
    /**
     * 
     */
    auction() {

    }
}